package com.syh.feignapi.config;

import com.syh.bscommon.Util.UserHolder;
import com.syh.bscommon.pojo.UserInfo;
import com.syh.feignapi.clients.fallback.CreditsClientFallback;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {
    @Bean
    public Logger.Level logLevel() {
        return Logger.Level.BASIC;
    }

    @Bean
    public CreditsClientFallback creditsClientFallback() {
        return new CreditsClientFallback();
    }

    @Bean
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                UserInfo userInfo = UserHolder.getUser();
                if (userInfo != null) {
                    requestTemplate.header("X-User-Id", String.valueOf(UserHolder.getUser().getId()))
                            .header("X-User-Email", UserHolder.getUser().getEmail())
                            .header("X-Company-Id", String.valueOf(UserHolder.getUser().getCompanyId()));
                }

            }
        };
    }
}
